package com.crazy.bookstore.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.crazy.bookstore.entity.Member;
import com.crazy.bookstore.entity.MemberReadState;
import com.crazy.bookstore.exception.ExceptionEnum;
import com.crazy.bookstore.exception.MyException;
import com.crazy.bookstore.mapper.MemberMapper;
import com.crazy.bookstore.mapper.MemberReadStateMapper;
import com.crazy.bookstore.service.MemberService;
import com.crazy.bookstore.utils.MD5Utils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @author: 卢辉宇
 * @date: 2022/12/29 20:53
 * @description:
 */
@Service
public class MemberServiceImpl implements MemberService {

    @Resource
    private MemberMapper memberMapper;

    @Resource
    private MemberReadStateMapper memberReadStateMapper;

    /**
     * 注册会员
     * @param username
     * @param password
     * @param nickname
     * @return
     */
    @Override
    public Member createMember(String username, String password, String nickname) throws MyException {
        /*实例化mybatisPuls的条件容器*/
        QueryWrapper<Member> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);

        /*根据用户名查询数据库*/
        List<Member> list = memberMapper.selectList(wrapper);
        if (list.size()>0){
            /*此用户名已经存在*/
            throw new MyException(ExceptionEnum.USERNAME_EXISTED);
        }

        Member member = new Member();
        member.setUsername(username);
//        member.setPassword(password); //要经过MD5加密
        member.setNickname(nickname);
        member.setCreateTime(new Date());
        /*随机生成0-999*/
        int salt=new Random().nextInt(1000)+1000;
        member.setSalt(salt);
        String md5 = MD5Utils.md5Digest(password, salt);
        member.setPassword(md5);

        /*调用MybatisPlus插入数据库*/
        memberMapper.insert(member);

        //清空密码和盐值
        member.setPassword(null);
        member.setSalt(null);
        return member;
    }

    /**
     * 检查登录逻辑
     * @param username
     * @param password
     * @return
     * @throws MyException
     */
    @Override
    public Member checkLogin(String username, String password) throws MyException {
        QueryWrapper<Member> wrapper = new QueryWrapper<>();
        wrapper.eq("username",username);
        Member member = memberMapper.selectOne(wrapper);
        if (member==null){
            //用户不存在
            throw new MyException(ExceptionEnum.USERNAME_NOT_EXISTED);
        }

        //调用MD5得到加密密码，与数据库的对比
        String md5 = MD5Utils.md5Digest(password, member.getSalt());
        if (!md5.equals(member.getPassword())){
            /*不匹配*/
            throw new MyException(ExceptionEnum.PASSWORD_ERROR);
        }

        //清空密码和盐值
        member.setPassword(null);
        member.setSalt(null);
        return member;
    }

    /**
     * 根据id查找会员
     * @param memberId
     * @return
     */
    @Override
    public Member selectById(Long memberId) {
        Member member = memberMapper.selectById(memberId);

        member.setPassword(null);
        member.setSalt(null);
        return member;
    }

    /**
     * 查询阅读状态
     * @param memberId
     * @param bookId
     * @return
     */
    @Override
    public MemberReadState selectMemberReadState(Long memberId,Long bookId){
        QueryWrapper<MemberReadState> wrapper = new QueryWrapper<>();
        wrapper.eq("member_id", memberId);
        wrapper.eq("book_id", bookId);

        MemberReadState memberReadState = memberReadStateMapper.selectOne(wrapper);

        return memberReadState;
    }

    /**
     * 更新阅读状态
     * @param memberId
     * @param bookId
     * @param readState
     * @return
     */
    @Override
    public MemberReadState updateMemberReadState(Long memberId,Long bookId,Integer readState){
        QueryWrapper<MemberReadState> wrapper = new QueryWrapper<>();
        wrapper.eq("member_id", memberId);
        wrapper.eq("book_id", bookId);

        MemberReadState memberReadState = memberReadStateMapper.selectOne(wrapper);
        if (memberReadState==null){
            /*数据库没有，则插入*/
            MemberReadState state = new MemberReadState();
            state.setMemberId(memberId);
            state.setBookId(bookId);
            state.setReadState(readState);
            state.setCreateTime(new Date());
            //插入数据库
            memberReadStateMapper.insert(state);
            memberReadState=state;
        }else {
            //已存在,则更新
            memberReadState.setReadState(readState);
            memberReadState.setCreateTime(new Date());
            memberReadStateMapper.updateById(memberReadState);
        }
        return memberReadState;
    }

}
